<!DOCTYPE html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script>
      run(async function () {
        await host.windowSize(360, 800, document.getElementById('webchat'));

        const baseActivity = {
          from: {
            id: 'bot',
            role: 'bot'
          },
          timestamp: new Date(2000, 0, 1, 12, 34, 56, 789).toISOString(),
          type: 'message'
        };

        WebChat.renderWebChat(
          {
            directLine: testHelpers.createDirectLineWithTranscript([
              {
                ...baseActivity,
                text: 'Hello, **World** without `webchat:fallback-text` field.',
                textFormat: 'markdown'
              },
              {
                ...baseActivity,
                channelData: {
                  'webchat:fallback-text': {}
                },
                text: 'Hello, **World** with invalid `webchat:fallback-text` field.',
                textFormat: 'markdown'
              },
              {
                ...baseActivity,
                channelData: {
                  'webchat:fallback-text': 'Display another thing.'
                },
                text: 'Display one thing.',
                textFormat: 'markdown'
              },
              {
                ...baseActivity,
                channelData: {
                  messageBack: {
                    displayText: 'Text from *MessageBack* (plain).'
                  }
                },
                from: { id: 'user', role: 'user' },
                text: 'This should not be displayed.',
                textFormat: 'plain'
              },
              {
                ...baseActivity,
                channelData: {
                  messageBack: {
                    displayText: 'Text from *MessageBack* (Markdown).'
                  }
                },
                from: { id: 'user', role: 'user' },
                text: 'This should not be displayed.',
                textFormat: 'markdown'
              },
              {
                ...baseActivity,
                channelData: { 'webchat:fallback-text': '' },
                text: 'This is presentational, should not be narrated.'
              },
              {
                ...baseActivity,
                text: 'This is *plain text*, preserve asterisks.',
                textFormat: 'plain'
              }
            ]),
            store: testHelpers.createStore(),
            styleOptions: {
              internalLiveRegionFadeAfter: 60000
            }
          },
          document.getElementById('webchat')
        );

        // SETUP: Render 7 activities.
        await pageConditions.uiConnected();
        await pageConditions.numActivitiesShown(7);
        await pageConditions.liveRegionStabilized();

        const screenReaderTexts = [].map.call(pageElements.liveRegion().children, child =>
          testHelpers.getAllTextContents(child).join('\n')
        );

        // THEN: One of the activity contains a fallback text of empty string. It become presentational and not rendered.
        expect(screenReaderTexts).toEqual([
          'connecting',
          // THEN: Without `webchat:fallback-text` field set, we will be narrating the "text" with best-effort on removing Markdown syntax.
          //       Timestamp should not be narrated.
          'Bot said:\nHello, World without webchat:fallback-text field.',
          // THEN: Without valid `webchat:fallback-text` field, we will be narrating the "text" with best-effort on removing Markdown syntax.
          'Bot said:\nHello, World with invalid webchat:fallback-text field.',
          // THEN: With `webchat:fallback-text` field set, we will be narrating it instead of the `text` field.
          'Bot said:\nDisplay another thing.',
          // THEN: With `channelData.messageBack.displayText` field set and `textFormat` is `plain`, we will be narrating it instead of the `text` field.
          'You said:\nText from *MessageBack* (plain).',
          // THEN: With `channelData.messageBack.displayText` field set, we will be narrating it instead of the `text` field, with best-effort on removing Markdown syntax.
          'You said:\nText from MessageBack (Markdown).',
          // THEN: With `textFormat` is set to `plain`, we should narrate it as-is.
          //       Also note we have a preceding activity with `webchat:fallback-text` field set as an empty string. That one is not narrated and considered presentational.
          'Bot said:\nThis is *plain text*, preserve asterisks.'
        ]);

        // THEN: The screenshot should show the message in Markdown format, i.e. <strong> and <code>.
        //       It must not show the text "This should not be displayed."
        await host.snapshot('local');
      });
    </script>
  </body>
</html>
